package test;
import java.util.LinkedList;
import java.util.Queue;

class MyStack {

    private Queue<Integer> q1;
    private Queue<Integer> q2;
    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }

    public void push(int x) {
        if(empty())
        {
            q1.offer(x);
            return;
        }
        if(!q1.isEmpty())
        {
            q1.offer(x);
        }
        else
        {
            q2.offer(x);
        }
    }

    public int pop() {
        if(empty())
        {
            return -1;
        }
        if(!q1.isEmpty())
        {
            int size = q1.size();
            for(int i = 0; i<size-1; i++)
            {
                q2.offer(q1.poll());
            }
            return q1.poll();
        }
        else{
            int size = q2.size();
            for(int i = 0; i<size-1; i++)
            {
                q1.offer(q2.poll());
            }
            return q2.poll();
        }
    }

    public int top() {
        if(empty())
        {
            return -1;
        }
        if(!q1.isEmpty())
        {
            int size = q1.size();
            int tmp = -1;
            for(int i = 0; i<size; i++)
            {
                tmp = q1.poll();
                q2.offer(tmp);
            }
            return tmp;
        }
        else{
            int size = q2.size();
            int tmp = -1;
            for(int i = 0; i<size; i++)
            {
                tmp = q2.poll();
                q1.offer(tmp);
            }
            return tmp;
        }
    }

    public boolean empty() {
        return q1.isEmpty()&&q2.isEmpty();
    }
}
public class demo {
    public static void main(String[] args) {
        MyStack s1 = new MyStack();
        s1.push(1);
        s1.push(2);
        s1.push(3);
        System.out.println(s1.top());
        System.out.println(s1.pop());
        System.out.println(s1.top());
    }
}
